package logic.facade;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import logic.service.TaskService;
import vo.IValueObject;
import vo.TaskVO;

/**
 *
 * @author Alexander
 */
public class TaskFacade extends Facade {

    public TaskFacade() {
        setService(new TaskService());
    }

    @Override
    protected boolean exist(IValueObject vo) {
        return !(super.find(((TaskVO) vo).getId()) == null);
    }

    public List<TaskVO> getTasksByCalendar(int id) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        List<TaskVO> list = null;
        try{
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            list = (List<TaskVO>) ((TaskService)getService()).getTasksByCalendar(id, em);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(em != null){
                em.clear();
                em.close();
            }
            return list;
        }
    }

    public List<TaskVO> getTaskByDate(Date date) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        List<TaskVO> list = null;
        try{
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            list = (List<TaskVO>) ((TaskService)getService()).getTasksByDate(date, em);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(em != null){
                em.clear();
                em.close();
            }
            return list;
        }
    }
}
